package cn.com.lyb.job.controller;


import cn.com.lyb.annotation.Retryable;
import cn.com.lyb.core.response.ResultDTO;
import cn.com.lyb.job.entity.JobDO;
import cn.com.lyb.job.service.IJobService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 任务表 前端控制器
 * </p>
 *
 * @author lvyuanbo
 * @since 2024-07-11
 */
@RestController
@RequestMapping("/job")
@Api(tags = "节点job")
public class JobController {

    @Autowired
    private IJobService jobService;


    @PostMapping("/create")
    @ApiOperation(value = "新增任务job")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "name", value = "job名称", required = true),
            @ApiImplicitParam(name = "type", value = "job类型", required = true),
            @ApiImplicitParam(name = "command", value = "job命令内容", required = true)
    })
    public ResultDTO create(String name, String type, String command){
        jobService.createEntity(name, type, command);
        return ResultDTO.success();
    }

    @PostMapping("/update")
    @ApiOperation(value = "修改任务job")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "id", required = true),
            @ApiImplicitParam(name = "name", value = "job名称", required = true),
            @ApiImplicitParam(name = "command", value = "job命令内容", required = true)
    })
    public ResultDTO update(Long id, String name, String command){
        jobService.updateEntity(id, name, command);
        return ResultDTO.success();
    }

    @PostMapping("/del")
    @ApiOperation(value = "删除任务job")
    @ApiImplicitParam(name = "ids", value = "ids", required = true)
    @Retryable(maxAttempts = 3, retryOn = {Exception.class})
    public ResultDTO del(String ids){
        jobService.deleteEntity(ids);
        return ResultDTO.success();
    }

    @GetMapping("/selectPage")
    @ApiOperation(value = "查询列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "start", value = "分页参数", required = true),
            @ApiImplicitParam(name = "length", value = "分页参数", required = true),
            @ApiImplicitParam(name = "searchVal", value = "搜索值")
    })
    public ResultDTO selectPage(String searchVal, Integer start, Integer length){
        List<JobDO> res = jobService.selectPage(searchVal, start, length);
        return ResultDTO.success(new PageInfo<>(res));
    }

    @PostMapping("/run")
    @ApiOperation(value = "运行任务")
    @ApiImplicitParam(name = "id", value = "id", required = true)
    public ResultDTO run(Long id){
        jobService.runTask(id);
        return ResultDTO.success();
    }
}
